package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.ShopDto;
import io.geekidea.boot.shop.entity.Shop;
import io.geekidea.boot.shop.mapper.ShopMapper;
import io.geekidea.boot.shop.query.app.AppShopQuery;
import io.geekidea.boot.shop.query.web.ShopQuery;
import io.geekidea.boot.shop.service.base.api.ShopService;
import io.geekidea.boot.shop.vo.app.AppShopVo;
import io.geekidea.boot.shop.vo.web.ShopVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addShop(ShopDto dto) {
        Shop shop = new Shop();
        BeanUtils.copyProperties(dto, shop);
        return save(shop);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateShop(ShopDto dto) {
        Integer id = dto.getId();
        Shop shop = getById(id);
        if (shop == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, shop);
        return updateById(shop);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteShop(Integer id) {
        return removeById(id);
    }

    @Override
    public ShopVo getShopById(Integer id) {
        return shopMapper.getShopById(id);
    }

    @Override
    public Paging<ShopVo> getShopPage(ShopQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<ShopVo> list = shopMapper.getShopPage(query);
        Paging<ShopVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppShopVo getAppShopById(Integer id) {
        return shopMapper.getAppShopById(id);
    }

    @Override
    public Paging<AppShopVo> getAppShopPage(AppShopQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppShopVo> list = shopMapper.getAppShopPage(query);
        Paging<AppShopVo> paging = new Paging<>(list);
        return paging;
    }

}
